package com.nononsenseapps.feeder.download

import android.content.Context
import com.gitee.wsl.service.AbstractDownLoadService
import com.gitee.wsl.service.AbstractDownLoadTask
import com.gitee.wsl.service.DownLoadTask
import com.gitee.wsl.service.TaskStatus
import ir.siaray.downloadmanagerplus.classes.Downloader
import ir.siaray.downloadmanagerplus.enums.DownloadStatus
import ir.siaray.downloadmanagerplus.enums.Errors
import ir.siaray.downloadmanagerplus.interfaces.ActionListener
import ir.siaray.downloadmanagerplus.model.DownloadItem

class DownloadManagerPlusService(val context: Context): AbstractDownLoadService() {

    override fun initServer() {
        Downloader.getDownloadsList(context).forEach{
            val downloader = Downloader.getInstance(context)
            downloader.setUrl(it.token)
            DownloadManagerPlusTask(downloader,it).let {task->
                task.warpAdapter(this)
                this.unCompletionTaskMap[task.downloadUrl] = task
            }
        }
    }

    override fun isDownLoaded(url: String): Boolean {
        TODO("Not yet implemented")
    }

    override fun getDownLoadState(url: String): TaskStatus {
        TODO("Not yet implemented")
    }

    override fun startDownLoad(url: String): DownLoadTask {
        TODO("Not yet implemented")
    }

    override fun startDimeDownLoad(url: String) {
        TODO("Not yet implemented")
    }

    override fun getDownLoad(url: String): DownLoadTask {
        TODO("Not yet implemented")
    }

    override fun stopDownLoad(url: String) {
        TODO("Not yet implemented")
    }

    override fun removeDownLoad(url: String) {
        TODO("Not yet implemented")
    }

}

val downLoadTaskStatMap= mutableMapOf(
    DownloadStatus.NONE to TaskStatus.UNKNOWN,
    DownloadStatus.CANCELED to TaskStatus.STOPPED,
    DownloadStatus.FAILED to TaskStatus.FAILED,
    DownloadStatus.PAUSED to TaskStatus.PAUSED,
    DownloadStatus.RUNNING to TaskStatus.RUNNING,
    DownloadStatus.SUCCESSFUL to TaskStatus.FINISHED,
    DownloadStatus.PENDING to TaskStatus.WAIT
)


class DownloadManagerPlusTask(private val downloader: Downloader, private val downloadItem:DownloadItem): AbstractDownLoadTask() {

    override val downloadUrl: String
        get() =downloadItem.token

    override val progressPoint: Int
        get() = downloadItem.percent

    override fun currentState(): TaskStatus {
        return downLoadTaskStatMap[downloadItem.downloadStatus]?: TaskStatus.UNKNOWN
    }

    override fun retry(): Int {
        start()
        return ++retryCount
    }

    override fun pause() {
        downloader.pause()
    }

    override fun cancel() {
        downloader.cancel(downloadItem.token)
    }

    override fun connectAble(): Boolean {
        return true
    }

    override fun delete() {
        downloader.deleteFile(downloadUrl,object:ActionListener{
            override fun onSuccess() {
               deleteFun?.invoke(this@DownloadManagerPlusTask)
            }

            override fun onFailure(error: Errors?) {
                TODO("Not yet implemented")
            }

        })
    }

    override fun start() {
        downloader.start()
    }

    override fun stop() {
        cancel()
    }

}